package com.ncmx.lmax.jms;

import java.util.Map;

import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

import lombok.extern.log4j.Log4j2;

import org.springframework.util.StringUtils;

import com.ncmx.lmax.engine.essentials.OrderEntry;
import com.ncmx.lmax.engine.essentials.OrderSide;
import com.ncmx.lmax.engine.essentials.ordertypes.PriceLimit;
import com.ncmx.lmax.engine.MatchingEngine;


@Log4j2
public class JmsReceiver implements MessageListener {

	@Resource(name = "symbolMatchingEngines")
	Map symbolMatchingEngines;

	@Override
	public void onMessage(Message message) {
		ObjectMessage objectMessage = (ObjectMessage) message;
		
		try {
			Order order = (Order) objectMessage.getObject();
			OrderEntry orderEntry = new OrderEntry.Builder(order.getOrderId(),OrderSide.valueOf(StringUtils.capitalize(order.getType().toLowerCase())), "Broker", order.getQty(), new PriceLimit(order.getPrice().doubleValue())).build();
			MatchingEngine matchingEngine=(MatchingEngine) symbolMatchingEngines.get(order.getSymbol());
			matchingEngine.publish(orderEntry);
			
			log.info("Message Recieved:" + order);
			

		} catch (JMSException e) {
			e.printStackTrace();
		}

	}
}
